<template>
  <li>
    <label>
      <input type="checkbox" :checked="todo.done" @change="change(todo.id)"/>

<!--      不建议直接修改props -->
<!--      <input type="checkbox" v-model="todo.done"/>-->
      <span>{{todo.title}}</span>
    </label>
    <button class="btn btn-danger" @click="deleteItem(todo.id)">删除</button>
  </li>
</template>

<script>
import pubsub  from "pubsub-js";
export default {
  name: "MyItem",
  //声明接收对象
  props: ['todo'],
  mounted() {
    console.log('item',this.todo)
  },
  methods: {
    change(id) {
      console.log('item组件，我改变了：',id)
      //通知app组件将对应的todo.done值取反
      pubsub.publish('checkTodo',id)
    },
    deleteItem(id) {
      if(confirm('确定要删除吗？')) {
        pubsub.publish('deleteTodo',id)
      }
    }
  }
}
</script>

<style scoped>

/*item*/
li {
  list-style: none;
  height: 36px;
  line-height: 36px;
  padding: 0 5px;
  border-bottom: 1px solid #ddd;
}

li label {
  float: left;
  cursor: pointer;
}

li label li input {
  vertical-align: middle;
  margin-right: 6px;
  position: relative;
  top: -1px;
}

li button {
  float: right;
  display: none;
  margin-top: 3px;
}

li:before {
  content: initial;
}

li:last-child {
  border-bottom: none;
}

li:hover button {
  display: block;
}
li:hover {
  background: #afa8a8;
}

</style>